<template>
  <VDialog @close="handleClose" :loading="loading" @save="save">
    <template #content>
      <div>
        <el-form
          ref="formRef"
          :model="form"
          :rules="rules"
          label-width="80"
          label-position="top"
        >
          <el-form-item>
            <div>
              <span class="mr-[8px]">已选择:</span>
              <span class="text-[#e50517] mr-[8px]">{{ ids?.length || 0 }}</span
              >数据
            </div>
          </el-form-item>
          <el-form-item label="审核" prop="state">
            <template v-if="option.length > 0">
              <el-radio-group v-model="form.state">
                <el-radio
                  :label="item.value"
                  v-for="item in option"
                  :key="item"
                  >{{ item.label }}</el-radio
                >
              </el-radio-group>
            </template>
            <template v-else>
              <el-radio-group v-model="form.state">
                <el-radio :label="1">通过</el-radio>
                <el-radio :label="2">拒绝</el-radio>
              </el-radio-group>
            </template>
          </el-form-item>
          <el-form-item label="审核备注" v-if="isRemark">
            <el-input
              v-model="form.remark"
              placeholder="请输入"
              :autosize="{ minRows: 4, maxRows: 10 }"
              type="textarea"
            />
          </el-form-item>
        </el-form>
      </div>
    </template>
  </VDialog>
</template>
<script setup>
import { ElMessage } from "element-plus";
import { review } from "./api.js";
const props = defineProps({
  ids: { type: Array, default: () => [] },
  url: { type: String, default: "" },
  option: { type: Object, default: () => [] },
  isRemark: { type: Boolean, default: true },
  method:{type:String,default:'post'}
});
const emit = defineEmits(["update:modelValue", "close", "refresh"]);
const loading = ref(false);
const { ids, url, option, isRemark,method } = toRefs(props);
const form = ref({});
const formRef = ref();
const rules = reactive({
  state: [{ required: true, message: "请选择审核状态", trigger: "change" }],
});
const save = async () => {
  await formRef.value.validate(async (valid, fields) => {
    if (valid) {
      try {
        loading.value = true;
        const { code, message } = await review(url.value, {
          ids: ids.value,
          state: form.value.state,
          remark: form.value.remark,
        },method.value);
        if (code !== 200) return;
        ElMessage.success("审核成功!");
        handleClose(true);
      } catch (error) {
      } finally {
        loading.value = false;
      }
    }
  });
};
const handleClose = async (refresh) => {
  form.value = {};
  await nextTick();
  formRef.value.resetFields();
  emit("update:modelValue", false);
  emit("close");
  if (refresh) emit("refresh");
};
defineExpose({ open });
</script>
